Notes/Domino 6 and 7 Forum

Notes/Domino 6 and 7 Forum





SMTP from JAVA Agent
~Vanessa Dwofreeskioden 01/05/2004 11:49 AM
Domino Designer All Releases All Platforms


I am calling a Java agent from a lotus notes agent to send email in HTML format. We used this code on v5.10 and it worked fine after updating the server to 6.0.3 it is not working correctly. Anyone have an ideas, help is greatly appreciated, more information below. It keeps giving an error of:

Agent error: java.net.SocketException: Connection reset by peer: socket closed

==============================================================================================

Below is the lotus notes code to call the java agent:

Set JavaAgent = db.GetAgent( "JavaEmail")

If Not(JavaAgent Is Nothing) Then
status = JavaAgent.RunOnServer (MailDoc.noteid)
End If

Set JavaAgent = Nothing

==============================================================================================

Below is the JavaAgent

import lotus.domino.*;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.util.Vector;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Agent agent = agentContext.getCurrentAgent();
Database db = agentContext.getCurrentDatabase();
String tmp_id = agent.getParameterDocID();
Document doc = db.getDocumentByID(agent.getParameterDocID());

// Vars for the socketed conenction to the SMTP server
Socket sock;
DataOutputStream dos;
byte[] b = null;

// Connect to the SMTP server

sock = new Socket( "127.0.0.1", 25 );

BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
dos = new DataOutputStream( sock.getOutputStream() );

// Say hello and reset. Reset not needed if everything OK but it does not hurt.
//Also if server doesn't respond that it will error out, not sending email.
br.readLine();
dos.writeBytes("HELO SERVER/COMPANY\n" );

// From needs to be an Valid SMTP address
dos.writeBytes("MAIL FROM: " + doc.getItemValueString("principal") + " \n");
br.readLine();

// Recipients. One line for each recipient.
br.readLine();

// Need to start data for text and header of the email.
dos.writeBytes("DATA\n");
br.readLine();

//This is the information that will show in the header section of the email. ( To, From and Subject )
dos.writeBytes("To: " + doc.getItemValueString("UserName") + "<" + doc.getItemValueString("sendTo") + "> \n");
dos.writeBytes("From: " + doc.getItemValueString("SentFrom") + "<" + doc.getItemValueString("principal") + "\n"); //" < " + doc.getItemValueString("from") +">\n");
dos.writeBytes("Subject: " + doc.getItemValueString("Subject") +" \n");

// Set the type to HTML
dos.writeBytes("Mime-Version: 1.0\n");
dos.writeBytes("Content-Type: text/html; charset=\"us-ascii\"\n\n");

// Add the Body and end it with full stop on its own line. Note: >1000 chars per line may cause problems.
dos.writeBytes(doc.getItemValueString("body") +" \n");
dos.writeBytes(".\n");

// We are done, quit
dos.writeBytes("QUIT\n");
br.readLine();

// Close the connection
dos.flush();
sock.close();

} catch(Exception e) {
e.printStackTrace();
}
}
}

Go back